package cn.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.common.ExceResult;
import cn.common.GridCommon;
import cn.common.LayTable;
import cn.filter.LoginCheck;
import cn.kpi.entity.sys.OrgEntity;
import cn.kpi.entity.sys.UserEntity;
import cn.kpi.entity.usualassessment.UsAssTemplateEntity;
import cn.kpi.entity.usualassessment.UserTemplateEntity;
import cn.kpi.entity.usualassessment.UserUsAssTempEntity;
import cn.kpi.service.sys.IOrgService;
import cn.kpi.service.sys.IUserService;
import cn.kpi.service.usualassessment.IUsAssTemplateService;
import cn.kpi.service.usualassessment.IUserUsAssTempService;

@Controller
@RequestMapping("userusasstemp")
public class UserUsAssTempController extends BaseController {

	@Autowired
	private IOrgService iOrgService;

	@Autowired
	private IUserUsAssTempService iUserUsAssTempService;

	@Autowired
	private IUsAssTemplateService iUsAssTemplateService;

	@Autowired
	private IUserService iUserService;

	@RequestMapping(value = "/user_usasstemp_list")
	public String UserUsassTempList(Model model) {
		try {
			//List<OrgEntity> orglst = iOrgService.GetOrgList();
			String where=this.GetLoginUserOrgidWhereForOrg();
			List<OrgEntity> orglst = iOrgService.GetOrgListByWhere(where);
			for (int i = 0; i < orglst.size(); i++) {
				String html = "";
				for (int j = 0; j < orglst.get(i).getLevel(); j++) {
					html += "&#12288;";
				}
				orglst.get(i).setOrgname(html + orglst.get(i).getOrgname());
			}
			model.addAttribute("orglst", orglst);
		} catch (Exception ex) {

		}
		return "usualassessment/user_usasstemp_list";
	}

	@RequestMapping(value = "/user_usasstemp_edit")
	public String UserUsassTempEdit(Model model) {
		try {
			String wheretemp="";
			if(this.GetLoginUserId()>0){
				String orgids=this.GetOrgListByRole();
				wheretemp+=" AND byuserid IN (SELECT userid FROM t_sys_user WHERE szdw IN ("+orgids+"))";
			}
			
			
			List<UsAssTemplateEntity> lsttemp = iUsAssTemplateService.GetUsAssTemplateListByWhere(wheretemp);
			model.addAttribute("lsttemp", lsttemp);

			List<UserEntity> lstuser = iUserService.GetUserListByWhere("");
			model.addAttribute("lstuser", lstuser);

			String where=this.GetLoginUserOrgidWhereForOrg();
			List<OrgEntity> orglst = iOrgService.GetOrgListByWhere(where);
			for (int i = 0; i < orglst.size(); i++) {
				String html = "";
				for (int j = 0; j < orglst.get(i).getLevel(); j++) {
					html += "&#12288;";
				}
				orglst.get(i).setOrgname(html + orglst.get(i).getOrgname());
			}
			model.addAttribute("orglst", orglst);
		} catch (Exception ex) {

		}
		return "usualassessment/user_usasstemp_edit";
	}

	@LoginCheck(description = true)
	@ResponseBody
	@RequestMapping(value = "/get_list")
	public LayTable<UserUsAssTempEntity> GetList(HttpServletRequest request) {

		List<UserUsAssTempEntity> lst = null;
		LayTable<UserUsAssTempEntity> layTable = new LayTable<UserUsAssTempEntity>();
		try {
			String realname = request.getParameter("realname");
			String szdw = request.getParameter("szdw");
			String where = "";
			if (realname != null && realname.length() > 0) {
				where += " AND realname LIKE '%" + realname + "%'";
			}
			if (szdw != null && szdw.length() > 0 && !szdw.equals("0")) {

				OrgEntity orgentity = iOrgService.GetOrgById(Long.parseLong(szdw));

				where += " AND szdw IN (SELECT orgid FROM t_sys_org WHERE lft>=" + orgentity.getLft() + " AND lft<="
						+ orgentity.getRgt() + ")";
			}

			where+=this.GetUserListByRole();
			
			int page=Integer.parseInt(request.getParameter("page"));
			int limit=Integer.parseInt(request.getParameter("limit"));
			int beginrow=(page-1)*limit;
			int endrow=limit;
			
			
			
			String limitwhere=where+" LIMIT "+beginrow+","+endrow;
			
			lst = iUserUsAssTempService.GetUserUsAssTempListByWhere(limitwhere);
			for (int i = 0; i < lst.size(); i++) {
				int numr=(page-1)*limit+(i + 1);
				lst.get(i).setRowNum(numr);
			}

			int iCount = iUserUsAssTempService.GetUserUsAssTempListCountByWhere(where);
			layTable.setCode(0);
			layTable.setCount(iCount);
			layTable.setData(lst);

		} catch (Exception e) {
			layTable.setCode(500);
		}
		return layTable;
	}

	@LoginCheck(description = true)
	@ResponseBody
	@RequestMapping(value = "/get_list_plyymb")
	public LayTable<UserUsAssTempEntity> GetListPlyymb(HttpServletRequest request) {

		List<UserUsAssTempEntity> lst = null;
		LayTable<UserUsAssTempEntity> layTable = new LayTable<UserUsAssTempEntity>();
		try {
			String realname = request.getParameter("realname");
			String szdw = request.getParameter("szdw");
			String where = "";
			if (realname != null && realname.length() > 0) {
				where += " AND realname LIKE '%" + realname + "%'";
			}
			if (szdw != null && szdw.length() > 0 && !szdw.equals("0")) {

				OrgEntity orgentity = iOrgService.GetOrgById(Long.parseLong(szdw));

				where += " AND szdw IN (SELECT orgid FROM t_sys_org WHERE lft>=" + orgentity.getLft() + " AND lft<="
						+ orgentity.getRgt() + ")";
			}

			where+=this.GetUserListByRole();
			
			int page=Integer.parseInt(request.getParameter("page"));
			int limit=Integer.parseInt(request.getParameter("limit"));
			int beginrow=(page-1)*limit;
			int endrow=limit;
			
			
			
			String limitwhere=where+" LIMIT "+beginrow+","+endrow;
			
			lst = iUserUsAssTempService.GetUserUsAssTempListByWhere(where);
			for (int i = 0; i < lst.size(); i++) {
				int numr=(page-1)*limit+(i + 1);
				lst.get(i).setRowNum(numr);
			}

			int iCount = iUserUsAssTempService.GetUserUsAssTempListCountByWhere(where);
			layTable.setCode(0);
			layTable.setCount(iCount);
			layTable.setData(lst);

		} catch (Exception e) {
			layTable.setCode(500);
		}
		return layTable;
	}
	
	@ResponseBody
	@RequestMapping(value = "/add_userusasstemp")
	public ExceResult AddUserUsAssTemp(String userids, Long templateid) {
		ExceResult result = new ExceResult();
		result.setCode(400);
		result.setResult(0);
		try {
			
			UsAssTemplateEntity tempentity=iUsAssTemplateService.GetUsAssTemplateListById(templateid);

			List<UserTemplateEntity> list = new ArrayList<UserTemplateEntity>();

			String[] userArr=userids.split(",");
			for(int i=0;i<userArr.length;i++){
				UserTemplateEntity entity=new UserTemplateEntity();
				entity.setUserid(Long.parseLong(userArr[i]));
				entity.setTemplateid(templateid);
				entity.setBegintime(tempentity.getBegintime());
				entity.setEndtime(tempentity.getEndtime());
				list.add(entity);
			}
			
			int iResult = iUserUsAssTempService.InsertBatchEntity(list);

			if (iResult > 0) {
				result.setCode(200);
				result.setResult(iResult);
				result.setMsg("操作成功");
			} else {
				result.setCode(300);
				result.setResult(iResult);
				result.setMsg("操作失败");
			}
		} catch (Exception ex) {
			result.setCode(500);
			result.setResult(0);
			result.setMsg("操作异常");
		}
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/get_list_org")
	public GridCommon<Map> GetListOrg(HttpServletRequest request) {
		GridCommon<Map> countlist = new GridCommon<Map>();
		try {
			
			String where=" AND orgid IN("+this.GetOrgListByRole()+")";
			List<Map> list = iUserUsAssTempService.GetUserTempOrgSummaryListByWhere(where);
			
			countlist.setCurrentPage(1);
			countlist.setPageSize(10);
			countlist.setItems(list);
			countlist.setStatus(0);
			countlist.setMsg("");
		} catch (Exception e) {
			countlist.setCurrentPage(1);
			countlist.setPageSize(10);
			countlist.setItems(null);
			countlist.setStatus(1);
			countlist.setMsg("操作成功");
		}
		return countlist;
	}
	
}
